# -*- coding: utf-8 -*-
from drc_core_python.schematic import Schematic, Component, Net

def check_rule(schematic: Schematic):
    """检查组件命名规则
    
    规则要求：
    1. 所有组件必须有名称
    2. IC必须以'U'开头
    3. 电阻必须以'R'开头
    4. 电容必须以'C'开头
    """
    violations = []
    
    # 检查所有组件
    for component in schematic.components:
        # 检查是否有名称
        if not component.name:
            violation = {
                'message': u'组件缺少名称 (类型: {})'.format(component.type),
                'severity': 'error',
                'component': component
            }
            violations.append(violation)
            continue
            
        # 检查命名前缀
        if component.type == 'ic' and not component.name.startswith('U'):
            violation = {
                'message': u'IC组件 {} 必须以"U"开头'.format(component.name),
                'severity': 'error',
                'component': component
            }
            violations.append(violation)
            
        elif component.type == 'resistor' and not component.name.startswith('R'):
            violation = {
                'message': u'电阻 {} 必须以"R"开头'.format(component.name),
                'severity': 'error',
                'component': component
            }
            violations.append(violation)
            
        elif component.type == 'capacitor' and not component.name.startswith('C'):
            violation = {
                'message': u'电容 {} 必须以"C"开头'.format(component.name),
                'severity': 'error',
                'component': component
            }
            violations.append(violation)
    
    return violations 